#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Any 类型转换测试用例

测试 any 类型（mc::variant）与强类型之间的转换功能，
验证生成的 C++ 代码能正确编译并运行。
"""

from typing import Any
from gtest import test, EXPECT_EQ, EXPECT_TRUE, EXPECT_FALSE
import copy


# ============================================================================
# 赋值转换测试
# ============================================================================


@test("AnyTypeConversion", "AssignmentIntConversion")
def test_any_to_int_assignment():
    """测试 any -> int 赋值转换"""
    x: Any = 42
    y: int = x
    EXPECT_EQ(y, 42)


@test("AnyTypeConversion", "AssignmentStringConversion")
def test_any_to_string_assignment():
    """测试 any -> string 赋值转换"""
    x: Any = "hello"
    y: str = x
    EXPECT_EQ(y, "hello")


@test("AnyTypeConversion", "AssignmentBoolConversion")
def test_any_to_bool_assignment():
    """测试 any -> bool 赋值转换"""
    x: Any = True
    y: bool = x
    EXPECT_TRUE(y)

    x2: Any = False
    y2: bool = x2
    EXPECT_FALSE(y2)


@test("AnyTypeConversion", "AssignmentDoubleConversion")
def test_any_to_double_assignment():
    """测试 any -> double 赋值转换"""
    x: Any = 3.14
    y: float = x
    EXPECT_EQ(y, 3.14)


@test("AnyTypeConversion", "AssignmentAnyToAny")
def test_any_to_any_assignment():
    """测试 any -> any 赋值（无需转换）"""
    x: Any = 100
    y: Any = x
    z: int = y
    EXPECT_EQ(z, 100)


@test("AnyTypeConversion", "AssignmentStrongToAny")
def test_strong_to_any_assignment():
    """测试强类型 -> any 赋值（隐式构造）"""
    x: int = 200
    y: Any = x
    z: int = y
    EXPECT_EQ(z, 200)


# ============================================================================
# 返回值转换测试（辅助函数）
# ============================================================================


def get_value_helper(data: Any) -> int:
    """辅助函数：返回 any -> int"""
    return data


def get_message_helper(data: Any) -> str:
    """辅助函数：返回 any -> string"""
    return data


def wrap_value_helper(value: int) -> Any:
    """辅助函数：返回 int -> any"""
    return value


# ============================================================================
# 返回值转换测试
# ============================================================================


@test("AnyTypeConversion", "ReturnIntConversion")
def test_return_int_conversion():
    """测试 any -> int 返回值转换"""
    result: int = get_value_helper(42)
    EXPECT_EQ(result, 42)


@test("AnyTypeConversion", "ReturnStringConversion")
def test_return_string_conversion():
    """测试 any -> string 返回值转换"""
    result: str = get_message_helper("test")
    EXPECT_EQ(result, "test")


@test("AnyTypeConversion", "ReturnAnyNoConversion")
def test_return_any_no_conversion():
    """测试强类型 -> any 返回值（无需转换）"""
    result: Any = wrap_value_helper(123)
    unwrapped: int = result
    EXPECT_EQ(unwrapped, 123)


# ============================================================================
# 比较运算测试（运算符重载）
# ============================================================================


@test("AnyTypeConversion", "ComparisonWithInt")
def test_any_comparison_with_int():
    """测试 any 与 int 的比较（运算符重载）"""
    x: Any = 42
    EXPECT_TRUE(x == 42)
    EXPECT_FALSE(x == 43)
    EXPECT_TRUE(x != 43)


@test("AnyTypeConversion", "ComparisonWithString")
def test_any_comparison_with_string():
    """测试 any 与 string 的比较（运算符重载）"""
    x: Any = "hello"
    EXPECT_TRUE(x == "hello")
    EXPECT_FALSE(x == "world")


@test("AnyTypeConversion", "ComparisonBetweenAny")
def test_any_comparison_between_any():
    """测试 any 之间的比较"""
    x: Any = 10
    y: Any = 10
    z: Any = 20

    EXPECT_TRUE(x == y)
    EXPECT_FALSE(x == z)


# ============================================================================
# 算术运算测试（运算符重载）
# ============================================================================


@test("AnyTypeConversion", "ArithmeticAddition")
def test_any_arithmetic_addition():
    """测试 any 的加法运算（运算符重载）"""
    x: Any = 10
    y: Any = 20
    z: Any = x + y

    result: int = z
    EXPECT_EQ(result, 30)


@test("AnyTypeConversion", "ArithmeticMixed")
def test_any_arithmetic_mixed():
    """测试 any 与强类型的混合运算"""
    x: Any = 5
    y: int = 10

    # any + int（运算符重载）
    z: Any = x + y
    result: int = z
    EXPECT_EQ(result, 15)


# ============================================================================
# Dict 访问返回 Any 测试
# ============================================================================


@test("AnyTypeConversion", "DictAccessReturnsAny")
def test_dict_access_returns_any():
    """测试 dict 索引访问返回 any 类型"""
    data: dict = {"port": 8080, "host": "localhost"}

    # dict["key"] 返回 any
    port_any: Any = data["port"]
    port_int: int = port_any
    EXPECT_EQ(port_int, 8080)

    host_any: Any = data["host"]
    host_str: str = host_any
    EXPECT_EQ(host_str, "localhost")


@test("AnyTypeConversion", "DictGetWithDefault")
def test_dict_get_with_default():
    """测试 dict.get() 返回 any 类型"""
    config: dict = {"timeout": 30}

    timeout_any: Any = config.get("timeout", 60)
    timeout_int: int = timeout_any
    EXPECT_EQ(timeout_int, 30)

    retry_any: Any = config.get("retry", 3)
    retry_int: int = retry_any
    EXPECT_EQ(retry_int, 3)


# ============================================================================
# 复杂场景测试
# ============================================================================


@test("AnyTypeConversion", "NestedConversion")
def test_nested_conversion():
    """测试嵌套的类型转换"""
    # 多层转换
    x: int = 42
    y: Any = x  # int -> any
    z: int = y  # any -> int
    w: Any = z  # int -> any
    result: int = w  # any -> int

    EXPECT_EQ(result, 42)


@test("AnyTypeConversion", "ChainedOperations")
def test_chained_operations():
    """测试链式操作"""
    x: Any = 10
    y: Any = 20
    z: Any = 30

    # 连续运算
    sum_val: Any = x + y + z
    result: int = sum_val
    EXPECT_EQ(result, 60)


@test("AnyTypeConversion", "ConditionalWithAny")
def test_conditional_with_any():
    """测试条件语句中的 any 类型"""
    x: Any = 100

    if x == 100:
        result: int = x
        EXPECT_EQ(result, 100)
    else:
        EXPECT_TRUE(False)  # 不应该执行到这里


def process_any_helper(value: Any) -> int:
    """辅助函数：处理 any 类型参数"""
    # any 参与运算（运算符重载）
    doubled: Any = value + value
    return doubled


@test("AnyTypeConversion", "FunctionWithAnyParameter")
def test_function_with_any_parameter():
    """测试带 any 参数的函数"""
    result: int = process_any_helper(21)
    EXPECT_EQ(result, 42)


@test("AnyTypeConversion", "MultipleAnyVariables")
def test_multiple_any_variables():
    """测试多个 any 变量的交互"""
    a: Any = 10
    b: Any = 20
    c: Any = 30

    # 组合运算
    sum1: Any = a + b  # 30
    sum2: Any = sum1 + c  # 60

    result: int = sum2
    EXPECT_EQ(result, 60)


# ============================================================================
# 边界情况测试
# ============================================================================


@test("AnyTypeConversion", "ZeroValue")
def test_zero_value():
    """测试零值的转换"""
    x: Any = 0
    y: int = x
    EXPECT_EQ(y, 0)


@test("AnyTypeConversion", "NegativeValue")
def test_negative_value():
    """测试负数的转换"""
    x: Any = -42
    y: int = x
    EXPECT_EQ(y, -42)


@test("AnyTypeConversion", "EmptyString")
def test_empty_string():
    """测试空字符串的转换"""
    x: Any = ""
    y: str = x
    EXPECT_EQ(y, "")


@test("AnyTypeConversion", "StringWithSpaces")
def test_string_with_spaces():
    """测试带空格的字符串"""
    x: Any = "hello world"
    y: str = x
    EXPECT_EQ(y, "hello world")


# ============================================================================
# 函数参数转换测试（强类型参数接收 any 类型实参）
# ============================================================================


def accept_int_helper(x: int) -> int:
    """辅助函数：接受强类型 int 参数"""
    return x + 1


def accept_str_helper(s: str) -> str:
    """辅助函数：接受强类型 str 参数"""
    return s + "!"


@test("AnyTypeConversion", "ParamConversionInt")
def test_param_conversion_int():
    """测试 any -> int 参数转换"""
    value: Any = 42
    result: int = accept_int_helper(value)
    EXPECT_EQ(result, 43)


@test("AnyTypeConversion", "ParamConversionString")
def test_param_conversion_string():
    """测试 any -> str 参数转换"""
    value: Any = "hello"
    result: str = accept_str_helper(value)
    EXPECT_EQ(result, "hello!")


@test("AnyTypeConversion", "ParamConversionFromDict")
def test_param_conversion_from_dict():
    """测试 dict 访问结果作为强类型参数"""
    d: dict = {"x": 10}
    result: int = accept_int_helper(d["x"])
    EXPECT_EQ(result, 11)


@test("AnyTypeConversion", "FunctionChain")
def test_function_chain():
    """测试函数嵌套调用（参数转换）"""
    value: Any = 41
    result: int = accept_int_helper(accept_int_helper(value))
    EXPECT_EQ(result, 43)  # 41 + 1 + 1


# ============================================================================
# Dict 链式访问测试
# ============================================================================


@test("AnyTypeConversion", "DictChainAccess")
def test_dict_chain_access():
    """测试 dict 多层嵌套访问"""
    d: dict = {"a": {"b": {"c": 100}}}
    temp1: dict = d["a"]
    temp2: dict = temp1["b"]
    result: int = temp2["c"]
    EXPECT_EQ(result, 100)

    result1: int = d["a"]["b"]["c"]
    EXPECT_EQ(result1, 100)

    result2: int = accept_int_helper(d["a"]["b"]["c"])
    EXPECT_EQ(result2, 101)


@test("AnyTypeConversion", "DictNestedToDict")
def test_dict_nested_to_dict():
    """测试嵌套 dict 的类型转换"""
    d: dict = {"outer": {"inner": 42}}
    inner: dict = d["outer"]
    result: int = inner["inner"]
    EXPECT_EQ(result, 42)


# ============================================================================
# 算术运算符测试（利用 variant 运算符重载）
# ============================================================================


@test("AnyTypeConversion", "ArithmeticSubtraction")
def test_arithmetic_subtraction():
    """测试减法运算"""
    x: Any = 100
    y: Any = 30
    result: int = x - y
    EXPECT_EQ(result, 70)


@test("AnyTypeConversion", "ArithmeticMultiplication")
def test_arithmetic_multiplication():
    """测试乘法运算"""
    x: Any = 6
    y: Any = 7
    result: int = x * y
    EXPECT_EQ(result, 42)


@test("AnyTypeConversion", "ArithmeticDivision")
def test_arithmetic_division():
    """测试除法运算"""
    x: Any = 84
    y: Any = 2
    result: int = x / y
    EXPECT_EQ(result, 42)


@test("AnyTypeConversion", "ArithmeticModulo")
def test_arithmetic_modulo():
    """测试取模运算"""
    x: Any = 10
    y: Any = 3
    result: int = x % y
    EXPECT_EQ(result, 1)


@test("AnyTypeConversion", "ArithmeticNegation")
def test_arithmetic_negation():
    """测试一元负号"""
    x: Any = 42
    result: int = -x
    EXPECT_EQ(result, -42)


# ============================================================================
# 比较运算符测试
# ============================================================================


@test("AnyTypeConversion", "ComparisonLessThan")
def test_comparison_less_than():
    """测试小于比较"""
    x: Any = 10
    y: Any = 20
    EXPECT_TRUE(x < y)
    EXPECT_FALSE(y < x)


@test("AnyTypeConversion", "ComparisonGreaterThan")
def test_comparison_greater_than():
    """测试大于比较"""
    x: Any = 20
    y: Any = 10
    EXPECT_TRUE(x > y)
    EXPECT_FALSE(y > x)


@test("AnyTypeConversion", "ComparisonLessEqual")
def test_comparison_less_equal():
    """测试小于等于比较"""
    x: Any = 10
    y: Any = 10
    z: Any = 20
    EXPECT_TRUE(x <= y)
    EXPECT_TRUE(x <= z)
    EXPECT_FALSE(z <= x)


@test("AnyTypeConversion", "ComparisonGreaterEqual")
def test_comparison_greater_equal():
    """测试大于等于比较"""
    x: Any = 10
    y: Any = 10
    z: Any = 5
    EXPECT_TRUE(x >= y)
    EXPECT_TRUE(x >= z)
    EXPECT_FALSE(z >= x)


@test("AnyTypeConversion", "ComparisonNotEqual")
def test_comparison_not_equal():
    """测试不等于比较"""
    x: Any = 10
    y: Any = 20
    EXPECT_TRUE(x != y)
    EXPECT_FALSE(x != x)


# ============================================================================
# 位运算符测试
# ============================================================================


@test("AnyTypeConversion", "BitwiseAnd")
def test_bitwise_and():
    """测试按位与运算"""
    x: Any = 12  # 1100
    y: Any = 10  # 1010
    result: int = x & y  # 1000 = 8
    EXPECT_EQ(result, 8)


@test("AnyTypeConversion", "BitwiseOr")
def test_bitwise_or():
    """测试按位或运算"""
    x: Any = 12  # 1100
    y: Any = 10  # 1010
    result: int = x | y  # 1110 = 14
    EXPECT_EQ(result, 14)


@test("AnyTypeConversion", "BitwiseXor")
def test_bitwise_xor():
    """测试按位异或运算"""
    x: Any = 12  # 1100
    y: Any = 10  # 1010
    result: int = x ^ y  # 0110 = 6
    EXPECT_EQ(result, 6)


@test("AnyTypeConversion", "BitwiseNot")
def test_bitwise_not():
    """测试按位取反运算"""
    x: Any = 0
    result: int = ~x
    EXPECT_EQ(result, -1)


@test("AnyTypeConversion", "BitwiseLeftShiftBothAny")
def test_bitwise_left_shift_both_any():
    """测试左移运算（both any）"""
    x: Any = 5
    y: Any = 2
    result: int = x << y  # any << any
    EXPECT_EQ(result, 20)


@test("AnyTypeConversion", "BitwiseLeftShiftAnyLeft")
def test_bitwise_left_shift_any_left():
    """测试左移运算（any 在左）"""
    x: Any = 5
    y: int = 2
    result: int = x << y  # any << int
    EXPECT_EQ(result, 20)


@test("AnyTypeConversion", "BitwiseLeftShiftAnyRight")
def test_bitwise_left_shift_any_right():
    """测试左移运算（any 在右）"""
    x: int = 5
    y: Any = 2
    result: int = x << y  # int << any
    EXPECT_EQ(result, 20)


@test("AnyTypeConversion", "BitwiseRightShiftBothAny")
def test_bitwise_right_shift_both_any():
    """测试右移运算（both any）"""
    x: Any = 20
    y: Any = 2
    result: int = x >> y  # any >> any
    EXPECT_EQ(result, 5)


@test("AnyTypeConversion", "BitwiseRightShiftAnyLeft")
def test_bitwise_right_shift_any_left():
    """测试右移运算（any 在左）"""
    x: Any = 20
    y: int = 2
    result: int = x >> y  # any >> int
    EXPECT_EQ(result, 5)


@test("AnyTypeConversion", "BitwiseRightShiftAnyRight")
def test_bitwise_right_shift_any_right():
    """测试右移运算（any 在右）"""
    x: int = 20
    y: Any = 2
    result: int = x >> y  # int >> any
    EXPECT_EQ(result, 5)


# ============================================================================
# 混合类型运算测试
# ============================================================================


@test("AnyTypeConversion", "MixedArithmeticAnyLeft")
def test_mixed_arithmetic_any_left():
    """测试 any 作为左操作数的混合算术"""
    x: Any = 10
    y: int = 5
    result1: int = x + y  # any + int
    result2: int = x - y  # any - int
    result3: int = x * y  # any * int
    result4: int = x / y  # any / int
    EXPECT_EQ(result1, 15)
    EXPECT_EQ(result2, 5)
    EXPECT_EQ(result3, 50)
    EXPECT_EQ(result4, 2)


@test("AnyTypeConversion", "MixedArithmeticAnyRight")
def test_mixed_arithmetic_any_right():
    """测试 any 作为右操作数的混合算术"""
    x: int = 10
    y: Any = 5
    result1: int = x + y  # int + any
    result2: int = x - y  # int - any
    result3: int = x * y  # int * any
    result4: int = x / y  # int / any
    EXPECT_EQ(result1, 15)
    EXPECT_EQ(result2, 5)
    EXPECT_EQ(result3, 50)
    EXPECT_EQ(result4, 2)


@test("AnyTypeConversion", "MixedComparisonAnyLeft")
def test_mixed_comparison_any_left():
    """测试 any 作为左操作数的比较"""
    x: Any = 42
    EXPECT_TRUE(x == 42)  # any == int
    EXPECT_TRUE(x != 43)  # any != int
    EXPECT_TRUE(x > 40)  # any > int
    EXPECT_TRUE(x < 50)  # any < int
    EXPECT_TRUE(x >= 42)  # any >= int
    EXPECT_TRUE(x <= 42)  # any <= int


@test("AnyTypeConversion", "MixedComparisonAnyRight")
def test_mixed_comparison_any_right():
    """测试 any 作为右操作数的比较"""
    y: Any = 42
    EXPECT_TRUE(42 == y)  # int == any
    EXPECT_TRUE(43 != y)  # int != any
    EXPECT_TRUE(50 > y)  # int > any
    EXPECT_TRUE(40 < y)  # int < any
    EXPECT_TRUE(42 >= y)  # int >= any
    EXPECT_TRUE(42 <= y)  # int <= any


@test("AnyTypeConversion", "MixedBitwiseAnyLeft")
def test_mixed_bitwise_any_left():
    """测试 any 作为左操作数的位运算"""
    x: Any = 12
    y: int = 10
    result1: int = x & y  # any & int
    result2: int = x | y  # any | int
    result3: int = x ^ y  # any ^ int
    EXPECT_EQ(result1, 8)
    EXPECT_EQ(result2, 14)
    EXPECT_EQ(result3, 6)


@test("AnyTypeConversion", "MixedBitwiseAnyRight")
def test_mixed_bitwise_any_right():
    """测试 any 作为右操作数的位运算"""
    x: int = 12
    y: Any = 10
    result1: int = x & y  # int & any
    result2: int = x | y  # int | any
    result3: int = x ^ y  # int ^ any
    EXPECT_EQ(result1, 8)
    EXPECT_EQ(result2, 14)
    EXPECT_EQ(result3, 6)


# ============================================================================
# 字符串连接测试
# ============================================================================


@test("AnyTypeConversion", "StringConcatenation")
def test_string_concatenation():
    """测试字符串连接"""
    x: Any = "hello"
    y: Any = " world"
    result: str = x + y
    EXPECT_EQ(result, "hello world")


@test("AnyTypeConversion", "StringWithInt")
def test_string_with_int():
    """测试字符串与数字的混合"""
    s: Any = "value: "
    n: int = 42
    # 注意：这可能需要显式转换
    result: str = s + str(n)
    EXPECT_EQ(result, "value: 42")


# ============================================================================
# 逻辑运算测试
# ============================================================================


@test("AnyTypeConversion", "LogicalNot")
def test_logical_not():
    """测试逻辑非运算"""
    x: Any = True
    y: Any = False
    EXPECT_FALSE(not x)
    EXPECT_TRUE(not y)


@test("AnyTypeConversion", "LogicalAnd")
def test_logical_and():
    """测试逻辑与运算"""
    x: Any = True
    y: Any = False
    EXPECT_TRUE(x and x)
    EXPECT_FALSE(x and y)
    EXPECT_FALSE(y and y)


@test("AnyTypeConversion", "LogicalOr")
def test_logical_or():
    """测试逻辑或运算"""
    x: Any = True
    y: Any = False
    EXPECT_TRUE(x or x)
    EXPECT_TRUE(x or y)
    EXPECT_FALSE(y or y)


# ============================================================================
# 三元表达式（条件表达式）测试
# ============================================================================


@test("AnyTypeConversion", "TernaryBothAny")
def test_ternary_both_any():
    """测试三元表达式（both any）"""
    x: Any = 10
    y: Any = 20
    result: int = x if x > y else y
    EXPECT_EQ(result, 20)


@test("AnyTypeConversion", "TernaryAnyInBody")
def test_ternary_any_in_body():
    """测试三元表达式（body 是 any）"""
    x: Any = 100
    y: int = 50
    result: int = x if x > 50 else y
    EXPECT_EQ(result, 100)


@test("AnyTypeConversion", "TernaryAnyInOrelse")
def test_ternary_any_in_orelse():
    """测试三元表达式（orelse 是 any）"""
    x: int = 10
    y: Any = 50
    result: int = x if x > 50 else y
    EXPECT_EQ(result, 50)
    EXPECT_EQ(x if x > 50 else y, 50)
    EXPECT_EQ(x if 50 <= x else y, 50)


# ============================================================================
# For 循环与 any 类型测试
# ============================================================================


@test("AnyTypeConversion", "ForLoopWithAnyElements")
def test_for_loop_with_any_elements():
    """测试使用列表推导式从 any 类型筛选元素"""
    numbers: list[Any] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    evens: list[int] = [x for x in numbers if x % 2 == 0]

    EXPECT_EQ(evens[0], 2)
    EXPECT_EQ(evens[1], 4)
    EXPECT_EQ(evens[4], 10)


@test("AnyTypeConversion", "ForLoopAnyCondition")
def test_for_loop_any_condition():
    """测试列表推导式中使用 any 类型条件"""
    values: list[Any] = [1, 2, 3, 4, 5]
    threshold: Any = 3

    result: list[int] = [x for x in values if x > threshold]

    EXPECT_EQ(result[0], 4)
    EXPECT_EQ(result[1], 5)


@test("AnyTypeConversion", "ForLoopAnyArithmetic")
def test_for_loop_any_arithmetic():
    """测试 for 循环中 any 类型的算术运算"""
    numbers: list[Any] = [1, 2, 3, 4, 5]

    # 累加 any 类型元素
    total: Any = 0
    for x in numbers:
        total = total + x  # any + any

    result: int = total
    EXPECT_EQ(result, 15)


# ============================================================================
# 列表推导式与 any 类型测试
# ============================================================================


@test("AnyTypeConversion", "ListComprehensionWithAny")
def test_list_comprehension_with_any():
    """测试列表推导式中使用 any 类型元素"""
    numbers: list[Any] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    # 筛选偶数（元素是 any 类型）
    evens: list[int] = [x for x in numbers if x % 2 == 0]
    expected_evens: list[int] = [2, 4, 6, 8, 10]
    EXPECT_EQ(evens, expected_evens)

    # 筛选并变换
    even_squares: list[int] = [x * x for x in numbers if x % 2 == 0]
    expected_squares: list[int] = [4, 16, 36, 64, 100]
    EXPECT_EQ(even_squares, expected_squares)


@test("AnyTypeConversion", "ListComprehensionAnyCondition")
def test_list_comprehension_any_condition():
    """测试列表推导式中 any 类型的条件判断"""
    values: list[Any] = [1, 2, 3, 4, 5]
    threshold: Any = 3

    # 使用 any 类型作为条件
    result: list[int] = [x for x in values if x > threshold]
    expected: list[int] = [4, 5]
    EXPECT_EQ(result, expected)


@test("AnyTypeConversion", "ListComprehensionAnyTransform")
def test_list_comprehension_any_transform():
    """测试列表推导式中 any 类型的转换"""
    numbers: list[int] = [1, 2, 3, 4, 5]

    # 转换为 any，再进行运算
    doubled: list[Any] = [x * 2 for x in numbers]

    # 从 any list 中提取强类型
    first: int = doubled[0]
    EXPECT_EQ(first, 2)


# ============================================================================
# 函数参数与期望类型测试
# ============================================================================


def accept_int_list(nums: list[int]) -> int:
    """接受 int 列表的函数"""
    total: int = 0
    for n in nums:
        total = total + n
    return total


def accept_any_list(items: list[Any]) -> Any:
    """接受 any 列表的函数"""
    if len(items) > 0:
        return items[0]
    return 0


@test("AnyTypeConversion", "FunctionParamListComprehension")
def test_function_param_list_comprehension():
    """测试函数参数中的列表推导式（期望类型传递）"""
    numbers: list[Any] = [1, 2, 3, 4, 5]

    # 列表推导式作为参数：期望类型应该是 list[int]
    result: int = accept_int_list([x for x in numbers if x % 2 == 0])
    EXPECT_EQ(result, 6)  # 2 + 4 = 6


@test("AnyTypeConversion", "FunctionParamListComprehensionToAny")
def test_function_param_list_comprehension_to_any():
    """测试传递列表推导式到 any 参数"""
    numbers: list[int] = [1, 2, 3, 4, 5]

    # 列表推导式作为参数：期望类型是 list[Any]
    first: Any = accept_any_list([x * 2 for x in numbers])
    result: int = first
    EXPECT_EQ(result, 2)


# ============================================================================
# 浮点数运算测试
# ============================================================================


@test("AnyTypeConversion", "FloatArithmeticAddition")
def test_float_arithmetic_addition():
    """测试浮点数加法运算"""
    x: Any = 3.14
    y: Any = 2.86
    result: float = x + y
    EXPECT_EQ(result, 6.0)


@test("AnyTypeConversion", "FloatArithmeticSubtraction")
def test_float_arithmetic_subtraction():
    """测试浮点数减法运算"""
    x: Any = 10.5
    y: Any = 3.5
    result: float = x - y
    EXPECT_EQ(result, 7.0)


@test("AnyTypeConversion", "FloatArithmeticMultiplication")
def test_float_arithmetic_multiplication():
    """测试浮点数乘法运算"""
    x: Any = 2.5
    y: Any = 4.0
    result: float = x * y
    EXPECT_EQ(result, 10.0)


@test("AnyTypeConversion", "FloatArithmeticDivision")
def test_float_arithmetic_division():
    """测试浮点数除法运算"""
    x: Any = 10.0
    y: Any = 2.5
    result: float = x / y
    EXPECT_EQ(result, 4.0)


@test("AnyTypeConversion", "FloatMixedWithInt")
def test_float_mixed_with_int():
    """测试浮点数与整数的混合运算"""
    x: Any = 3.5
    y: int = 2
    result: float = x * y
    EXPECT_EQ(result, 7.0)


@test("AnyTypeConversion", "NegativeFloat")
def test_negative_float():
    """测试负浮点数"""
    x: Any = -3.14
    y: float = x
    EXPECT_EQ(y, -3.14)


@test("AnyTypeConversion", "ZeroFloat")
def test_zero_float():
    """测试零值浮点数"""
    x: Any = 0.0
    y: float = x
    EXPECT_EQ(y, 0.0)


# ============================================================================
# 增强赋值运算符测试
# ============================================================================


@test("AnyTypeConversion", "AugmentedAssignmentAddInt")
def test_augmented_assignment_add_int():
    """测试 += 运算符（int）"""
    x: Any = 10
    x += 5
    result: int = x
    EXPECT_EQ(result, 15)


@test("AnyTypeConversion", "AugmentedAssignmentSubtract")
def test_augmented_assignment_subtract():
    """测试 -= 运算符"""
    x: Any = 20
    x -= 5
    result: int = x
    EXPECT_EQ(result, 15)


@test("AnyTypeConversion", "AugmentedAssignmentMultiply")
def test_augmented_assignment_multiply():
    """测试 *= 运算符"""
    x: Any = 3
    x *= 4
    result: int = x
    EXPECT_EQ(result, 12)


@test("AnyTypeConversion", "AugmentedAssignmentDivide")
def test_augmented_assignment_divide():
    """测试 /= 运算符"""
    x: Any = 20
    x /= 4
    result: int = x
    EXPECT_EQ(result, 5)


@test("AnyTypeConversion", "AugmentedAssignmentModulo")
def test_augmented_assignment_modulo():
    """测试 %= 运算符"""
    x: Any = 10
    x %= 3
    result: int = x
    EXPECT_EQ(result, 1)


@test("AnyTypeConversion", "AugmentedAssignmentBitwiseAnd")
def test_augmented_assignment_bitwise_and():
    """测试 &= 运算符"""
    x: Any = 12
    x &= 10
    result: int = x
    EXPECT_EQ(result, 8)


@test("AnyTypeConversion", "AugmentedAssignmentBitwiseOr")
def test_augmented_assignment_bitwise_or():
    """测试 |= 运算符"""
    x: Any = 12
    x |= 10
    result: int = x
    EXPECT_EQ(result, 14)


@test("AnyTypeConversion", "AugmentedAssignmentBitwiseXor")
def test_augmented_assignment_bitwise_xor():
    """测试 ^= 运算符"""
    x: Any = 12
    x ^= 10
    result: int = x
    EXPECT_EQ(result, 6)


@test("AnyTypeConversion", "AugmentedAssignmentLeftShift")
def test_augmented_assignment_left_shift():
    """测试 <<= 运算符"""
    x: Any = 5
    x <<= 2
    result: int = x
    EXPECT_EQ(result, 20)


@test("AnyTypeConversion", "AugmentedAssignmentRightShift")
def test_augmented_assignment_right_shift():
    """测试 >>= 运算符"""
    x: Any = 20
    x >>= 2
    result: int = x
    EXPECT_EQ(result, 5)


@test("AnyTypeConversion", "AugmentedAssignmentMixedTypes")
def test_augmented_assignment_mixed_types():
    """测试增强赋值混合类型（any += int）"""
    x: Any = 10
    y: int = 5
    x += y
    result: int = x
    EXPECT_EQ(result, 15)


@test("AnyTypeConversion", "AugmentedAssignmentIntPlusAny")
def test_augmented_assignment_int_plus_any():
    """测试增强赋值：int += Any"""
    x: int = 10
    y: Any = 5
    x += y  # int += any
    EXPECT_EQ(x, 15)


@test("AnyTypeConversion", "AugmentedAssignmentIntMinusAny")
def test_augmented_assignment_int_minus_any():
    """测试增强赋值：int -= Any"""
    x: int = 20
    y: Any = 5
    x -= y  # int -= any
    EXPECT_EQ(x, 15)


@test("AnyTypeConversion", "AugmentedAssignmentIntMultiplyAny")
def test_augmented_assignment_int_multiply_any():
    """测试增强赋值：int *= Any"""
    x: int = 3
    y: Any = 4
    x *= y  # int *= any
    EXPECT_EQ(x, 12)


@test("AnyTypeConversion", "AugmentedAssignmentIntDivideAny")
def test_augmented_assignment_int_divide_any():
    """测试增强赋值：int /= Any"""
    x: int = 20
    y: Any = 4
    x /= y  # int /= any
    EXPECT_EQ(x, 5)


# ============================================================================
# While 循环测试
# ============================================================================


@test("AnyTypeConversion", "WhileLoopWithAnyCounter")
def test_while_loop_with_any_counter():
    """测试 while 循环与 any 类型计数器"""
    count: Any = 0
    total: int = 0

    while count < 5:
        total = total + 1
        count = count + 1

    EXPECT_EQ(total, 5)
    result: int = count
    EXPECT_EQ(result, 5)


@test("AnyTypeConversion", "WhileLoopAnyCondition")
def test_while_loop_any_condition():
    """测试 while 循环的 any 类型条件"""
    threshold: Any = 10
    value: int = 0

    while value < threshold:
        value = value + 2

    EXPECT_EQ(value, 10)


@test("AnyTypeConversion", "WhileLoopAnyAccumulator")
def test_while_loop_any_accumulator():
    """测试 while 循环中的 any 类型累加器"""
    i: int = 1
    sum_val: Any = 0

    while i <= 5:
        sum_val = sum_val + i
        i = i + 1

    result: int = sum_val
    EXPECT_EQ(result, 15)


# ============================================================================
# List[Any] 的方法操作测试
# ============================================================================


@test("AnyTypeConversion", "ListAnyAppend")
def test_list_any_append():
    """测试 list[Any] 的 append 方法"""
    items: list[Any] = []
    items.append(42)
    items.append("hello")
    items.append(3.14)

    val1: int = items[0]
    val2: str = items[1]
    val3: float = items[2]

    EXPECT_EQ(val1, 42)
    EXPECT_EQ(val2, "hello")
    EXPECT_EQ(val3, 3.14)


@test("AnyTypeConversion", "ListAnyExtend")
def test_list_any_extend():
    """测试 list[Any] 的 extend 方法"""
    items: list[Any] = [1, 2]
    items.extend([3, 4, 5])

    EXPECT_EQ(len(items), 5)
    val: int = items[4]
    EXPECT_EQ(val, 5)


@test("AnyTypeConversion", "ListAnyPop")
def test_list_any_pop():
    """测试 list[Any] 的 pop 方法"""
    items: list[Any] = [10, 20, 30]
    last: Any = items.pop()
    result: int = last
    EXPECT_EQ(result, 30)
    EXPECT_EQ(len(items), 2)


@test("AnyTypeConversion", "ListAnyIndexAccess")
def test_list_any_index_access():
    """测试 list[Any] 的索引访问"""
    numbers: list[Any] = [10, 20, 30, 40, 50]

    first: int = numbers[0]
    third: int = numbers[2]
    last: int = numbers[4]

    EXPECT_EQ(first, 10)
    EXPECT_EQ(third, 30)
    EXPECT_EQ(last, 50)


@test("AnyTypeConversion", "ListAnyNegativeIndex")
def test_list_any_negative_index():
    """测试 list[Any] 的负索引访问"""
    numbers: list[Any] = [10, 20, 30]

    last: int = numbers[-1]
    second_last: int = numbers[-2]

    EXPECT_EQ(last, 30)
    EXPECT_EQ(second_last, 20)


# ============================================================================
# Any 类型作为索引下标测试
# ============================================================================


@test("AnyTypeConversion", "AnyAsListIndex")
def test_any_as_list_index():
    """测试 Any 类型作为 list 索引"""
    numbers: list[int] = [10, 20, 30, 40, 50]

    # Any 类型的正索引
    idx: Any = 2
    result1: int = numbers[idx]
    EXPECT_EQ(result1, 30)

    # Any 类型的负索引
    idx_neg: Any = -1
    result2: int = numbers[idx_neg]
    EXPECT_EQ(result2, 50)

    # Any 类型索引为 0
    idx_zero: Any = 0
    result3: int = numbers[idx_zero]
    EXPECT_EQ(result3, 10)


@test("AnyTypeConversion", "AnyAsListAnyIndex")
def test_any_as_list_any_index():
    """测试 Any 类型作为 list[Any] 索引"""
    items: list[Any] = [100, 200, 300, 400]

    # Any 索引访问 list[Any]
    idx: Any = 1
    result: int = items[idx]
    EXPECT_EQ(result, 200)

    # Any 负索引
    idx_neg: Any = -2
    result_neg: int = items[idx_neg]
    EXPECT_EQ(result_neg, 300)


@test("AnyTypeConversion", "AnyAsDictKey")
def test_any_as_dict_key():
    """测试 Any 类型作为 dict 键"""
    data: dict = {"name": "Alice", "age": 30, "city": "Beijing"}

    # Any 类型的字符串键
    key: Any = "name"
    result1: str = data[key]
    EXPECT_EQ(result1, "Alice")

    # Any 类型的另一个键
    key2: Any = "age"
    result2: int = data[key2]
    EXPECT_EQ(result2, 30)


@test("AnyTypeConversion", "AnyIndexInExpression")
def test_any_index_in_expression():
    """测试 Any 索引在表达式中使用"""
    values: list[int] = [5, 10, 15, 20, 25]

    # Any 索引参与算术运算
    idx: Any = 2
    result: int = values[idx] + 100
    EXPECT_EQ(result, 115)

    # Any 索引用于比较
    idx2: Any = 4
    EXPECT_TRUE(values[idx2] > 20)


@test("AnyTypeConversion", "AnyIndexFromComputation")
def test_any_index_from_computation():
    """测试计算得出的 Any 索引"""
    numbers: list[int] = [100, 200, 300, 400, 500]

    # 通过运算得到 Any 索引
    base: Any = 1
    offset: Any = 2
    idx: Any = base + offset  # 3

    result: int = numbers[idx]
    EXPECT_EQ(result, 400)


@test("AnyTypeConversion", "AnyIndexChained")
def test_any_index_chained():
    """测试链式 Any 索引访问"""
    matrix: list[list[int]] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

    # 第一层索引是 Any
    row_idx: Any = 1
    row: list[int] = matrix[row_idx]

    # 第二层索引也是 Any
    col_idx: Any = 2
    value: int = row[col_idx]
    EXPECT_EQ(value, 6)

    # 链式访问
    idx1: Any = 0
    idx2: Any = 2
    direct: int = matrix[idx1][idx2]
    EXPECT_EQ(direct, 3)


@test("AnyTypeConversion", "AnyIndexInLoop")
def test_any_index_in_loop():
    """测试循环中使用 Any 索引"""
    data: list[int] = [10, 20, 30, 40, 50]

    total: int = 0
    i: Any = 0

    while i < 5:
        total = total + data[i]  # Any 作为索引
        i = i + 1

    EXPECT_EQ(total, 150)


@test("AnyTypeConversion", "AnyIndexAssignment")
def test_any_index_assignment():
    """测试使用 Any 索引进行赋值"""
    numbers: list[int] = [1, 2, 3, 4, 5]

    # 使用 Any 索引赋值
    idx: Any = 2
    numbers[idx] = 100

    EXPECT_EQ(numbers[2], 100)
    EXPECT_EQ(numbers[0], 1)
    EXPECT_EQ(numbers[4], 5)


@test("AnyTypeConversion", "AnyIndexDeletion")
def test_any_index_deletion():
    """测试使用 Any 索引删除元素"""
    numbers: list[int] = [10, 20, 30, 40, 50]

    # 使用 Any 索引删除
    idx: Any = 2
    del numbers[idx]

    expected: list[int] = [10, 20, 40, 50]
    EXPECT_EQ(numbers, expected)


# ============================================================================
# Any 在布尔上下文中的隐式转换测试
# ============================================================================


@test("AnyTypeConversion", "AnyAsBoolCondition")
def test_any_as_bool_condition():
    """测试 any 类型在 if 条件中的隐式布尔转换"""
    x: Any = 0
    y: Any = 1

    if x:
        EXPECT_TRUE(False)  # 不应该执行
    else:
        EXPECT_TRUE(True)

    if y:
        EXPECT_TRUE(True)
    else:
        EXPECT_TRUE(False)  # 不应该执行


@test("AnyTypeConversion", "AnyBoolInWhile")
def test_any_bool_in_while():
    """测试 any 类型作为 while 循环条件"""
    running: Any = True
    count: int = 0

    while running:
        count = count + 1
        if count >= 3:
            running = False

    EXPECT_EQ(count, 3)


@test("AnyTypeConversion", "AnyEmptyStringBool")
def test_any_empty_string_bool():
    """测试 any 类型空字符串的布尔值"""
    empty: Any = ""
    non_empty: Any = "hello"

    if empty:
        EXPECT_TRUE(False)  # 不应该执行
    else:
        EXPECT_TRUE(True)

    if non_empty:
        EXPECT_TRUE(True)
    else:
        EXPECT_TRUE(False)  # 不应该执行


# ============================================================================
# 成员运算符测试 (in/not in)
# ============================================================================


@test("AnyTypeConversion", "InOperatorWithAnyList")
def test_in_operator_with_any_list():
    """测试 in 运算符与 list[Any]"""
    values: list[Any] = [1, 2, 3, 4, 5]
    x: Any = 3

    EXPECT_TRUE(x in values)
    EXPECT_FALSE(x not in values)


@test("AnyTypeConversion", "NotInOperatorWithAnyList")
def test_not_in_operator_with_any_list():
    """测试 not in 运算符与 list[Any]"""
    values: list[Any] = [1, 2, 3, 4, 5]
    x: Any = 10

    EXPECT_TRUE(x not in values)
    EXPECT_FALSE(x in values)


@test("AnyTypeConversion", "InOperatorIntInAnyList")
def test_in_operator_int_in_any_list():
    """测试 int 值在 list[Any] 中查找"""
    values: list[Any] = [10, 20, 30]

    EXPECT_TRUE(20 in values)
    EXPECT_FALSE(40 in values)


@test("AnyTypeConversion", "InOperatorAnyInIntList")
def test_in_operator_any_in_int_list():
    """测试 any 值在 list[int] 中查找"""
    values: list[int] = [10, 20, 30]
    x: Any = 20

    EXPECT_TRUE(x in values)
    EXPECT_FALSE(x not in values)


@test("AnyTypeConversion", "InOperatorStringAny")
def test_in_operator_string_any():
    """测试 any 字符串与 in 运算符"""
    names: list[Any] = ["alice", "bob", "charlie"]
    name: Any = "bob"

    EXPECT_TRUE(name in names)
    EXPECT_FALSE("dave" in names)


# ============================================================================
# 更复杂的嵌套容器测试
# ============================================================================


@test("AnyTypeConversion", "NestedListAny")
def test_nested_list_any():
    """测试嵌套列表 list[list[Any]]"""
    matrix: list[list[Any]] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

    row: list[Any] = matrix[1]
    value: int = row[1]
    EXPECT_EQ(value, 5)

    # 直接链式访问
    direct: int = matrix[0][2]
    EXPECT_EQ(direct, 3)


@test("AnyTypeConversion", "NestedDictWithAnyList")
def test_nested_dict_with_any_list():
    """测试嵌套 dict 与 list[Any]"""
    data: dict = {"numbers": [1, 2, 3], "strings": ["a", "b", "c"]}

    # 注意：从弱类型 dict 中取出，需要显式标注正确的类型
    numbers: list[int] = data["numbers"]
    first_num: int = numbers[0]
    EXPECT_EQ(first_num, 1)

    strings: list[str] = data["strings"]
    first_str: str = strings[0]
    EXPECT_EQ(first_str, "a")


@test("AnyTypeConversion", "ListOfDictsAny")
def test_list_of_dicts_any():
    """测试 list[dict] 包含 any 值"""
    users: list[dict] = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]

    first_user: dict = users[0]
    name: str = first_user["name"]
    age: int = first_user["age"]

    EXPECT_EQ(name, "Alice")
    EXPECT_EQ(age, 30)


@test("AnyTypeConversion", "ThreeLevelNesting")
def test_three_level_nesting():
    """测试三层嵌套访问"""
    data: dict = {"level1": {"level2": {"level3": [100, 200, 300]}}}

    level1: dict = data["level1"]
    level2: dict = level1["level2"]
    level3: list[int] = level2["level3"]
    value: int = level3[1]

    EXPECT_EQ(value, 200)

    # 链式访问
    direct_value: int = data["level1"]["level2"]["level3"][2]
    EXPECT_EQ(direct_value, 300)


# ============================================================================
# 综合场景测试
# ============================================================================


@test("AnyTypeConversion", "ComplexMixedOperations")
def test_complex_mixed_operations():
    """测试复杂的混合操作场景"""
    config: dict = {"base": 10, "multiplier": 2}

    base: Any = config["base"]
    multiplier: Any = config.get("multiplier", 1)

    result: Any = base * multiplier
    result += 5

    final: int = result
    EXPECT_EQ(final, 25)


@test("AnyTypeConversion", "AnyInListComprehensionFilter")
def test_any_in_list_comprehension_filter():
    """测试 any 在列表推导式的复杂过滤中"""
    data: list[Any] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    threshold: Any = 5
    multiplier: Any = 2

    # 过滤并转换
    result: list[int] = [x * multiplier for x in data if x > threshold]
    expected: list[int] = [12, 14, 16, 18, 20]

    EXPECT_EQ(result, expected)


@test("AnyTypeConversion", "AnyWithMultipleAugmentedOps")
def test_any_with_multiple_augmented_ops():
    """测试 any 的多次增强赋值操作"""
    x: Any = 10

    x += 5  # 15
    x *= 2  # 30
    x -= 10  # 20
    x /= 4  # 5

    result: int = x
    EXPECT_EQ(result, 5)


# ============================================================================
# Set 与 Any 类型交互测试
# ============================================================================


@test("AnyTypeConversion", "SetAnyCreation")
def test_set_any_creation():
    """测试 set[Any] 的创建"""
    # 创建包含混合类型元素的 set[Any]
    # 注意：避免使用 True/False，因为它们可能与 1/0 重复
    items: set[Any] = {1, "hello", 3.14, 2}
    EXPECT_EQ(len(items), 4)


@test("AnyTypeConversion", "SetAnyAdd")
def test_set_any_add():
    """测试 set[Any] 的 add 方法"""
    items: set[Any] = {1, 2, 3}
    items.add(4)
    items.add("hello")
    items.add(5.5)

    EXPECT_EQ(len(items), 6)
    EXPECT_TRUE(4 in items)
    EXPECT_TRUE("hello" in items)


@test("AnyTypeConversion", "SetAnyElementAccess")
def test_set_any_element_access():
    """测试 set[Any] 元素访问与类型转换"""
    items: set[Any] = {42, "test", 3.14}

    # 通过 in 检查存在性
    EXPECT_TRUE(42 in items)
    EXPECT_TRUE("test" in items)
    EXPECT_TRUE(3.14 in items)
    EXPECT_FALSE(100 in items)


@test("AnyTypeConversion", "SetAnyIteration")
def test_set_any_iteration():
    """测试 set[Any] 的迭代"""
    numbers: set[Any] = {1, 2, 3, 4, 5}

    total: Any = 0
    for x in numbers:
        total = total + x

    result: int = total
    EXPECT_EQ(result, 15)


@test("AnyTypeConversion", "SetAnyUnion")
def test_set_any_union():
    """测试 set[Any] 的并集操作"""
    s1: set[Any] = {1, 2, 3}
    s2: set[Any] = {3, 4, 5}

    result: set[Any] = s1 | s2
    EXPECT_EQ(len(result), 5)
    EXPECT_TRUE(1 in result)
    EXPECT_TRUE(5 in result)


@test("AnyTypeConversion", "SetAnyIntersection")
def test_set_any_intersection():
    """测试 set[Any] 的交集操作"""
    s1: set[Any] = {1, 2, 3, 4}
    s2: set[Any] = {3, 4, 5, 6}

    result: set[Any] = s1 & s2
    EXPECT_EQ(len(result), 2)
    EXPECT_TRUE(3 in result)
    EXPECT_TRUE(4 in result)


@test("AnyTypeConversion", "SetAnyDifference")
def test_set_any_difference():
    """测试 set[Any] 的差集操作"""
    s1: set[Any] = {1, 2, 3, 4}
    s2: set[Any] = {3, 4, 5}

    result: set[Any] = s1 - s2
    EXPECT_EQ(len(result), 2)
    EXPECT_TRUE(1 in result)
    EXPECT_TRUE(2 in result)


@test("AnyTypeConversion", "SetToAny")
def test_set_to_any():
    """测试 set 赋值给 Any"""
    s: set[int] = {1, 2, 3}
    x: Any = s

    # Any 中存储的 set 可以被转回 set
    s2: set[int] = x
    EXPECT_EQ(len(s2), 3)
    EXPECT_TRUE(2 in s2)


@test("AnyTypeConversion", "AnyToSet")
def test_any_to_set():
    """测试 Any 赋值给 set"""
    x: Any = {1, 2, 3, 4, 5}
    s: set[int] = x

    EXPECT_EQ(len(s), 5)
    EXPECT_TRUE(3 in s)
    EXPECT_FALSE(10 in s)


@test("AnyTypeConversion", "SetAnyInDict")
def test_set_any_in_dict():
    """测试 set[Any] 存储在 dict 中"""
    data: dict = {"items": {1, 2, 3, 4, 5}}

    items: set[Any] = data["items"]
    EXPECT_EQ(len(items), 5)
    EXPECT_TRUE(3 in items)


@test("AnyTypeConversion", "SetAnyFromList")
def test_set_any_from_list():
    """测试从 list[Any] 创建 set[Any]"""
    numbers: list[Any] = [1, 2, 2, 3, 3, 3, 4]
    unique: set[Any] = set(numbers)

    EXPECT_EQ(len(unique), 4)
    EXPECT_TRUE(1 in unique)
    EXPECT_TRUE(4 in unique)


@test("AnyTypeConversion", "SetAnyToList")
def test_set_any_to_list():
    """测试 set[Any] 转换为 list[Any]"""
    items: set[Any] = {3, 1, 4, 5, 9, 2}
    lst: list[Any] = list(items)

    EXPECT_EQ(len(lst), 6)
    # set 转 list 后顺序不定，只检查长度


@test("AnyTypeConversion", "SetAnyUpdate")
def test_set_any_update():
    """测试 set[Any] 的 update 方法"""
    s1: set[Any] = {1, 2, 3}
    s2: set[Any] = {3, 4, 5}

    s1.update(s2)
    EXPECT_EQ(len(s1), 5)
    EXPECT_TRUE(5 in s1)


@test("AnyTypeConversion", "SetAnyRemove")
def test_set_any_remove():
    """测试 set[Any] 的 remove 方法"""
    items: set[Any] = {1, 2, 3, 4, 5}
    items.remove(3)

    EXPECT_EQ(len(items), 4)
    EXPECT_FALSE(3 in items)
    EXPECT_TRUE(4 in items)


@test("AnyTypeConversion", "SetAnyDiscard")
def test_set_any_discard():
    """测试 set[Any] 的 discard 方法"""
    items: set[Any] = {10, 20, 30}
    items.discard(20)
    items.discard(999)  # 不存在的元素，不会抛出异常

    EXPECT_EQ(len(items), 2)
    EXPECT_FALSE(20 in items)


@test("AnyTypeConversion", "SetAnyPop")
def test_set_any_pop():
    """测试 set[Any] 的 pop 方法"""
    items: set[Any] = {100, 200, 300}
    item: Any = items.pop()

    # pop 返回 Any 类型，可以转换为具体类型
    EXPECT_EQ(len(items), 2)


@test("AnyTypeConversion", "SetAnyClear")
def test_set_any_clear():
    """测试 set[Any] 的 clear 方法"""
    items: set[Any] = {1, 2, 3, 4, 5}
    items.clear()

    EXPECT_EQ(len(items), 0)


@test("AnyTypeConversion", "SetAnyIssubset")
def test_set_any_issubset():
    """测试 set[Any] 的 issubset 方法"""
    s1: set[Any] = {1, 2}
    s2: set[Any] = {1, 2, 3, 4}

    EXPECT_TRUE(s1.issubset(s2))
    EXPECT_FALSE(s2.issubset(s1))


@test("AnyTypeConversion", "SetAnyIssuperset")
def test_set_any_issuperset():
    """测试 set[Any] 的 issuperset 方法"""
    s1: set[Any] = {1, 2, 3, 4}
    s2: set[Any] = {2, 3}

    EXPECT_TRUE(s1.issuperset(s2))
    EXPECT_FALSE(s2.issuperset(s1))


@test("AnyTypeConversion", "SetAnyIsdisjoint")
def test_set_any_isdisjoint():
    """测试 set[Any] 的 isdisjoint 方法"""
    s1: set[Any] = {1, 2, 3}
    s2: set[Any] = {4, 5, 6}
    s3: set[Any] = {3, 4, 5}

    EXPECT_TRUE(s1.isdisjoint(s2))
    EXPECT_FALSE(s1.isdisjoint(s3))


@test("AnyTypeConversion", "SetAnyComparison")
def test_set_any_comparison():
    """测试 set[Any] 的比较操作"""
    s1: set[Any] = {1, 2, 3}
    s2: set[Any] = {1, 2, 3}
    s3: set[Any] = {1, 2, 4}

    EXPECT_TRUE(s1 == s2)
    EXPECT_FALSE(s1 == s3)
    EXPECT_TRUE(s1 != s3)


@test("AnyTypeConversion", "SetAnyMixedTypes")
def test_set_any_mixed_types():
    """测试 set[Any] 包含混合类型元素"""
    # 注意：避免使用 True/False，因为它们可能与 1/0 重复
    mixed: set[Any] = {42, "hello", 3.14, 99, 100}

    EXPECT_EQ(len(mixed), 5)
    EXPECT_TRUE(42 in mixed)
    EXPECT_TRUE("hello" in mixed)
    EXPECT_TRUE(3.14 in mixed)


@test("AnyTypeConversion", "SetAnyComprehension")
def test_set_any_comprehension():
    """测试 set[Any] 推导式"""
    numbers: list[Any] = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
    unique: set[Any] = {x for x in numbers}

    EXPECT_EQ(len(unique), 4)
    EXPECT_TRUE(1 in unique)
    EXPECT_TRUE(4 in unique)


@test("AnyTypeConversion", "SetAnyFilterComprehension")
def test_set_any_filter_comprehension():
    """测试 set[Any] 带过滤的推导式"""
    numbers: list[Any] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    evens: set[Any] = {x for x in numbers if x % 2 == 0}

    EXPECT_EQ(len(evens), 5)
    EXPECT_TRUE(2 in evens)
    EXPECT_TRUE(10 in evens)
    EXPECT_FALSE(1 in evens)


@test("AnyTypeConversion", "SetAnyNestedInList")
def test_set_any_nested_in_list():
    """测试 list 包含 set[Any]"""
    sets: list[set[Any]] = [{1, 2}, {3, 4}, {5, 6}]

    first: set[Any] = sets[0]
    EXPECT_EQ(len(first), 2)
    EXPECT_TRUE(1 in first)

    second: set[Any] = sets[1]
    EXPECT_TRUE(3 in second)


@test("AnyTypeConversion", "SetAnyDeepCopy")
def test_set_any_deep_copy():
    """测试 set[Any] 通过 Any 类型的深拷贝"""
    # 创建嵌套结构：set 中包含 list
    items: set[Any] = {42, "hello"}
    items.add(items)  # 循环引用自己

    # 通过 Any 类型存储 set
    container: Any = items

    # 深拷贝
    deep_copied: Any = copy.deepcopy(container)

    # 验证深拷贝创建了独立副本
    copied_set: set[Any] = deep_copied
    copied_set.add(999)

    EXPECT_EQ(len(items), 3)  # 原 set 不受影响
    EXPECT_EQ(len(copied_set), 4)  # 拷贝的 set 已修改
    EXPECT_TRUE(999 in copied_set)
    EXPECT_FALSE(999 in items)


# ============================================================================
# 复杂嵌套结构与深拷贝测试
# ============================================================================


@test("DeepCopyComplex", "NestedListDeepCopy")
def test_nested_list_deep_copy():
    """测试嵌套列表的深拷贝"""
    # 创建嵌套列表
    inner1: list[int] = [1, 2, 3]
    inner2: list[int] = [4, 5, 6]
    outer: list[list[int]] = [inner1, inner2]

    # 浅拷贝：修改内层列表会影响原列表
    shallow: list[list[int]] = copy.copy(outer)
    shallow[0].append(999)
    EXPECT_EQ(len(outer[0]), 4)  # 原列表被修改
    EXPECT_EQ(outer[0][3], 999)

    # 深拷贝：修改内层列表不会影响原列表
    outer2: list[list[int]] = [[1, 2, 3], [4, 5, 6]]
    deep: list[list[int]] = copy.deepcopy(outer2)
    deep[0].append(888)
    EXPECT_EQ(len(outer2[0]), 3)  # 原列表未被修改
    EXPECT_EQ(len(deep[0]), 4)  # 拷贝已修改


@test("DeepCopyComplex", "DictContainingList")
def test_dict_containing_list():
    """测试 dict 包含 list 的深拷贝"""

    # 创建 dict 包含 list
    data: dict = {"numbers": [10, 20, 30], "name": "test"}

    # 浅拷贝：顶层 dict 独立，但内层 list 共享
    shallow: dict = copy.copy(data)

    # 在不清楚类型推导引擎推导的 numbers 是什么类型的情况下，可以不给定模板参数
    # 使用弱类型绑定，这仍然可以维持与原有 list 的引用关系
    #
    # 注意：如果强制转换成某个强类型有可能会引发一次类型转换从而是创建新的数组
    # 而不是共享引用，所以这种情况推荐使用弱类型绑定避免不必要的类型转换
    numbers: list = shallow["numbers"]
    numbers.append(40)

    original_numbers: list = data["numbers"]
    EXPECT_EQ(len(original_numbers), 4)  # 原 list 被修改

    # 深拷贝：完全独立
    data2: dict = {"numbers": [10, 20, 30], "name": "test"}
    deep: dict = copy.deepcopy(data2)
    deep_numbers: list = deep["numbers"]
    deep_numbers.append(50)

    original_numbers2: list = data2["numbers"]
    EXPECT_EQ(len(original_numbers2), 3)  # 原 list 未被修改
    EXPECT_EQ(len(deep_numbers), 4)


@test("DeepCopyComplex", "ListContainingDict")
def test_list_containing_dict():
    """测试 list 包含 dict 的深拷贝"""

    # 创建 list 包含 dict
    d1: dict = {"name": "Alice", "age": 30}
    d2: dict = {"name": "Bob", "age": 25}
    users: list[dict] = [d1, d2]

    # 浅拷贝：修改内层 dict 会影响原列表
    shallow: list[dict] = copy.copy(users)
    first: dict = shallow[0]
    first["age"] = 31

    original_first: dict = users[0]
    EXPECT_EQ(original_first["age"], 31)  # 原 dict 被修改

    # 深拷贝：完全独立
    users2: list[dict] = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]
    deep: list[dict] = copy.deepcopy(users2)
    deep_first: dict = deep[0]
    deep_first["age"] = 32

    original_first2: dict = users2[0]
    EXPECT_EQ(original_first2["age"], 30)  # 原 dict 未被修改
    EXPECT_EQ(deep_first["age"], 32)


@test("DeepCopyComplex", "ComplexNestedStructure")
def test_complex_nested_structure():
    """测试复杂嵌套结构：dict -> list -> dict -> list"""

    # 创建复杂嵌套结构
    inner_list: list[int] = [1, 2, 3]
    inner_dict: dict = {"data": inner_list}
    middle_list: list[Any] = [inner_dict, 100]
    outer_dict: dict = {"nested": middle_list, "value": 999}

    # 深拷贝
    deep: dict = copy.deepcopy(outer_dict)

    # 验证初始状态相等
    EXPECT_EQ(deep["value"], 999)
    nested_list: list[Any] = deep["nested"]
    nested_dict: dict = nested_list[0]
    # 注意：虽然从 dict 中取出，但实际类型是 list[int]
    nested_data: list[int] = nested_dict["data"]
    EXPECT_EQ(nested_data[0], 1)

    # 修改深拷贝不影响原始数据
    nested_data.append(111)  # 修改拷贝的列表
    original_nested: list[Any] = outer_dict["nested"]
    original_dict: dict = original_nested[0]
    original_data: list[int] = original_dict["data"]
    EXPECT_EQ(len(original_data), 3)  # 原始数据未被修改
    EXPECT_EQ(len(nested_data), 4)  # 拷贝已修改


# ============================================================================
# 循环引用测试
# ============================================================================


@test("CyclicReference", "DictSelfReference")
def test_dict_self_reference():
    """测试 dict 自引用的深拷贝"""

    # 创建自引用的 dict
    d: dict = {"key1": 123}
    d["self"] = d

    # 深拷贝应该保持循环结构
    d_copy: dict = copy.deepcopy(d)

    # 验证拷贝成功
    EXPECT_EQ(d_copy["key1"], 123)

    # 验证循环引用保持
    self_dict: dict = d_copy["self"]
    EXPECT_EQ(self_dict["key1"], 123)

    # 修改拷贝不应该影响原始对象
    d_copy["key1"] = 456
    EXPECT_EQ(d["key1"], 123)
    EXPECT_EQ(d_copy["key1"], 456)


@test("CyclicReference", "DictMutualReference")
def test_dict_mutual_reference():
    """测试 dict 相互引用的深拷贝"""

    # 创建相互引用的 dict
    d1: dict = {"name": "d1"}
    d2: dict = {"name": "d2"}
    d1["ref"] = d2
    d2["ref"] = d1

    # 深拷贝
    d1_copy: dict = copy.deepcopy(d1)

    # 验证拷贝成功
    EXPECT_EQ(d1_copy["name"], "d1")
    d2_copy: dict = d1_copy["ref"]
    EXPECT_EQ(d2_copy["name"], "d2")

    # 验证循环引用保持
    d1_back: dict = d2_copy["ref"]
    EXPECT_EQ(d1_back["name"], "d1")

    # 修改拷贝不影响原始对象
    d1_copy["name"] = "d1_copy"
    EXPECT_EQ(d1["name"], "d1")
    EXPECT_EQ(d1_copy["name"], "d1_copy")


@test("CyclicReference", "ListWithDictCyclicRef")
def test_list_with_dict_cyclic_ref():
    """测试 list 和 dict 的循环引用"""

    # 创建 dict 包含 list，list 又引用回 dict
    d: dict = {"key": 100}
    lst: list[Any] = [1, 2, 3]
    d["list"] = lst
    lst.append(d)  # list 引用 dict

    # 深拷贝
    d_copy: dict = copy.deepcopy(d)

    # 验证拷贝成功
    EXPECT_EQ(d_copy["key"], 100)
    lst_copy: list[Any] = d_copy["list"]
    EXPECT_EQ(lst_copy[0], 1)

    # 验证循环引用保持
    d_back: dict = lst_copy[3]
    EXPECT_EQ(d_back["key"], 100)

    # 修改拷贝不影响原始对象
    d_copy["key"] = 200
    EXPECT_EQ(d["key"], 100)
    EXPECT_EQ(d_copy["key"], 200)


@test("CyclicReference", "ThreeWayCyclicRef")
def test_three_way_cyclic_ref():
    """测试三层循环引用（d1 -> d2 -> d3 -> d1）"""

    d1: dict = {"name": "d1"}
    d2: dict = {"name": "d2"}
    d3: dict = {"name": "d3"}

    d1["next"] = d2
    d2["next"] = d3
    d3["next"] = d1  # 形成循环

    # 深拷贝
    d1_copy: dict = copy.deepcopy(d1)

    # 验证拷贝成功
    EXPECT_EQ(d1_copy["name"], "d1")

    d2_copy: dict = d1_copy["next"]
    EXPECT_EQ(d2_copy["name"], "d2")

    d3_copy: dict = d2_copy["next"]
    EXPECT_EQ(d3_copy["name"], "d3")

    d1_back: dict = d3_copy["next"]
    EXPECT_EQ(d1_back["name"], "d1")

    # 修改拷贝不影响原始对象
    d1_copy["name"] = "d1_modified"
    EXPECT_EQ(d1["name"], "d1")
    EXPECT_EQ(d1_back["name"], "d1_modified")


# ============================================================================
# Any 类型与深拷贝的交互
# ============================================================================


@test("AnyDeepCopy", "AnyStoringList")
def test_any_storing_list():
    """测试 Any 存储 list 的深拷贝"""

    # Any 存储嵌套列表
    inner: list[int] = [1, 2, 3]
    outer: list[list[int]] = [inner, [4, 5, 6]]
    any_list: Any = outer

    # 深拷贝 Any
    any_copy: Any = copy.deepcopy(any_list)

    # 提取并验证
    outer_copy: list[list[int]] = any_copy
    outer_copy[0].append(999)

    # 原始数据未被修改
    EXPECT_EQ(len(outer[0]), 3)
    EXPECT_EQ(len(outer_copy[0]), 4)


@test("AnyDeepCopy", "AnyStoringDict")
def test_any_storing_dict():
    """测试 Any 存储 dict 的深拷贝"""

    # Any 存储嵌套 dict
    inner: dict = {"value": 100}
    outer: dict = {"nested": inner, "name": "test"}
    any_dict: Any = outer

    # 深拷贝 Any
    any_copy: Any = copy.deepcopy(any_dict)

    # 提取并验证
    outer_copy: dict = any_copy
    nested: dict = outer_copy["nested"]
    nested["value"] = 200

    # 原始数据未被修改
    original_nested: dict = outer["nested"]
    EXPECT_EQ(original_nested["value"], 100)
    EXPECT_EQ(nested["value"], 200)


@test("AnyDeepCopy", "AnyWithCyclicRef")
def test_any_with_cyclic_ref():
    """测试 Any 存储循环引用结构的深拷贝"""

    # 创建循环引用结构并存储到 Any
    d: dict = {"key": 42}
    d["self"] = d
    any_dict: Any = d

    # 深拷贝 Any
    any_copy: Any = copy.deepcopy(any_dict)

    # 提取并验证
    d_copy: dict = any_copy
    EXPECT_EQ(d_copy["key"], 42)

    # 验证循环引用保持
    self_ref: dict = d_copy["self"]
    EXPECT_EQ(self_ref["key"], 42)

    # 修改拷贝不影响原始
    d_copy["key"] = 999
    EXPECT_EQ(d["key"], 42)
    EXPECT_EQ(self_ref["key"], 999)


@test("AnyDeepCopy", "ListOfAnyDeepCopy")
def test_list_of_any_deep_copy():
    """测试 list[Any] 的深拷贝"""

    # 创建包含不同类型的 list[Any]
    inner_dict: dict = {"value": 100}
    items: list[Any] = [42, "hello", inner_dict, [1, 2, 3]]

    # 深拷贝
    items_copy: list[Any] = copy.deepcopy(items)

    # 修改拷贝中的 dict
    copied_dict: dict = items_copy[2]
    copied_dict["value"] = 200

    # 原始数据未被修改
    EXPECT_EQ(inner_dict["value"], 100)
    EXPECT_EQ(copied_dict["value"], 200)

    # 修改拷贝中的 list
    # 注意：虽然从 list[Any] 中取出，但实际类型是 list[int]
    copied_list: list[int] = items_copy[3]
    copied_list.append(4)

    original_list: list[int] = items[3]
    EXPECT_EQ(len(original_list), 3)
    EXPECT_EQ(len(copied_list), 4)


# ============================================================================
# 混合类型复杂场景
# ============================================================================


@test("ComplexScenarios", "DictListSetMixed")
def test_dict_list_set_mixed():
    """测试 dict、list、set 混合的复杂结构"""

    # 创建混合结构
    s: set[int] = {1, 2, 3}
    inner_dict: dict = {"set_data": s, "value": 42}
    lst: list[Any] = [inner_dict, "test", 100]
    outer: dict = {"list": lst, "name": "complex"}

    # 深拷贝
    outer_copy: dict = copy.deepcopy(outer)

    # 提取各层数据
    list_copy: list[Any] = outer_copy["list"]
    dict_copy: dict = list_copy[0]
    set_copy: set[int] = dict_copy["set_data"]

    # 修改拷贝不影响原始
    set_copy.add(4)
    dict_copy["value"] = 999
    list_copy.append("new")

    EXPECT_EQ(len(s), 3)
    EXPECT_EQ(len(set_copy), 4)
    EXPECT_EQ(inner_dict["value"], 42)
    EXPECT_EQ(dict_copy["value"], 999)
    EXPECT_EQ(len(lst), 3)
    EXPECT_EQ(len(list_copy), 4)


@test("ComplexScenarios", "DeepNestingWithAny")
def test_deep_nesting_with_any():
    """测试深度嵌套结构"""

    # 创建深度嵌套：10 层 dict
    current: Any = 0
    for i in range(10):
        d: dict = {"level": i, "data": current}
        current = d

    # 深拷贝
    current_copy: Any = copy.deepcopy(current)

    # 验证每一层
    temp_any: Any = current_copy
    for i in range(9, -1, -1):
        temp: dict = temp_any
        EXPECT_EQ(temp["level"], i)
        if i > 0:
            temp_any = temp["data"]

    # 修改最外层
    current_dict: dict = current
    current_dict["level"] = 999

    copy_dict: dict = current_copy
    EXPECT_EQ(copy_dict["level"], 9)  # 拷贝未被修改


@test("ComplexScenarios", "EmptyContainersDeepCopy")
def test_empty_containers_deep_copy():
    """测试空容器的深拷贝"""

    # 空容器
    empty_list: list[int] = []
    empty_dict: dict = {}
    empty_set: set[int] = set()

    # 深拷贝
    list_copy: list[int] = copy.deepcopy(empty_list)
    dict_copy: dict = copy.deepcopy(empty_dict)
    set_copy: set[int] = copy.deepcopy(empty_set)

    # 验证都是空的
    EXPECT_EQ(len(list_copy), 0)
    EXPECT_EQ(len(dict_copy), 0)
    EXPECT_EQ(len(set_copy), 0)

    # 修改拷贝不影响原始
    list_copy.append(1)
    dict_copy["key"] = "value"
    set_copy.add(1)

    EXPECT_EQ(len(empty_list), 0)
    EXPECT_EQ(len(empty_dict), 0)
    EXPECT_EQ(len(empty_set), 0)


@test("ComplexScenarios", "MixedTypesInAnyList")
def test_mixed_types_in_any_list():
    """测试 Any 列表包含多种引用类型"""

    # 创建包含不同引用类型的列表
    d1: dict = {"a": 1}
    d2: dict = {"b": 2}
    l1: list[int] = [10, 20]
    l2: list[int] = [30, 40]

    mixed: list[Any] = [d1, l1, d2, l2]

    # 浅拷贝
    shallow: list[Any] = copy.copy(mixed)
    shallow_d1: dict = shallow[0]
    shallow_d1["a"] = 100
    EXPECT_EQ(d1["a"], 100)  # 原始被修改

    # 深拷贝
    mixed2: list[Any] = [{"a": 1}, [10, 20], {"b": 2}, [30, 40]]
    deep: list[Any] = copy.deepcopy(mixed2)
    deep_d1: dict = deep[0]
    deep_d1["a"] = 999

    original_d1: dict = mixed2[0]
    EXPECT_EQ(original_d1["a"], 1)  # 原始未被修改
    EXPECT_EQ(deep_d1["a"], 999)
